#include <iostream>
#include <iomanip>
#include <fstream>
#include "String.h"
#include <cstring>
using std::strcmp;
using std::strcpy;
using std::strcat;

using std::setw;
String::String(const char*s):length((s!=0)?strlen(s):0)
{
        setString(s);
}
String::String(const String &copy):length(copy.length)
{
        setString(copy.sPtr);
}
String::~String()
{
        delete [] sPtr;
}
const String &String::operator =(const String &right)
{
        if(&right!=this)
        {
                delete [] sPtr;
                (*this).length=right.length;
                setString(right.sPtr);
        }
        else
                std::cout<<"Nu poti asigna acelasi obiect\n";
        return *this;
}
const String &String::operator +=(const String &right)
{
        size_t newLength=(*this).length+right.length;
        char *temp=new char[newLength+1];
        strcpy(temp,(*this).sPtr);
        strcpy(temp+(*this).length,right.sPtr);
        delete [] sPtr;
        (*this).sPtr=temp;
        (*this).length=newLength;
        return *this;
}
bool String::operator!()const
{
        return (*this).length==0;
}
bool String::operator==(const String &right)const
{
        return strcmp((*this).sPtr,right.sPtr)==0;
}
bool String::operator<(const String &right)const
{
        return strcmp((*this).sPtr,right.sPtr)<0;
}
char &String::operator[](int sub)
{       
        if(sub<0||sub>=length)
        {
                std::cerr<<"eroare";
                
        }
        return (*this).sPtr[sub];
}
char String::operator[](int sub)const
{
        if(sub<0||sub>=(*this).length)
        {
                std::cerr<<"eroare";
        }
        return (*this).sPtr[sub];
}
String String::operator()(int index,int subLen)const
{
        if(index<0||index>=(*this).length||subLen<0)
        {
                std::cerr<<"eroare";
        }
        int len;
        if((subLen==0)||(index+subLen)>(*this).length)
                len=(*this).length-index;
        else
                len=subLen;
        char *temp=new char[len+1];
        strncpy(temp,&(*this).sPtr[index],len);
        temp[len]='\n';
        String tempString(temp);
        delete [] temp;
        return tempString;
}
int String::getLength()const
{
        return (*this).length;
}
void String::setString(const char *str2)
{
        (*this).sPtr=new char[(*this).length+1];
        if(str2!=0)
                strcpy((*this).sPtr,str2);
        else
                (*this).sPtr[0]='\0';
}
std::ostream &operator<<(std::ostream &out,const String &obj)
{
        out<<obj.sPtr;
        return out;
}
std::istream &operator >>(std::istream &inp,String &obj)
{
        char temp[100];
        inp>>setw(100)>>temp;
        obj=temp;
        return inp;
}
bool String::operator>(const String& right)const{
                return right<*this;
}
bool String::operator!=(const String& right)const{      
                return !(*this==right);
}
bool String::operator <=(const String& right)const{
                return !(right<*this);          
}
bool String::operator >=(const String& right)const{
                return !(*this<right);
}

